package com.gxa.domain.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("product_info")  // 指定表名（如果表名与类名不一致）
public class Product {

        @TableId(value = "serial_no", type = IdType.AUTO)  // 主键自增
        private Integer serialNo;

        @TableField("barcode_no")  // 字段名与数据库列名一致时可省略
        private String barcodeNo;

        @TableField("product_name")
        private String productName;

        @TableField("selling_price")
        private BigDecimal sellingPrice;  // 使用BigDecimal处理金额

        private String unit;

        @TableField("stock_quantity")
        private Integer stockQuantity;

        private ProductStatus status;  // 使用枚举类型

        @TableField("stock_in_time")
        private Date stockInTime;

        @TableField("operator_id")
        private Integer operatorId;  // 关联employee表的employee_id

        @TableField("first_level_category")
        private Integer firstLevelCategory;  // 关联product_category表的category_no

        @TableField("second_level_category")
        private Integer secondLevelCategory;  // 关联product_category表的category_no

        // 枚举类定义状态
        @JsonSerialize(using = ProductStatusSerializer.class)
        public enum ProductStatus {
                UNLOCKED("未锁定"),
                LOCKED("锁定");

                private final String desc;

                ProductStatus(String desc) {
                        this.desc = desc;
                }

                // 获取描述文本（中文）
                public String getDesc() {
                        return desc;
                }

                // 从数据库字符串值转换为枚举
                public static ProductStatus fromName(String name) {
                        for (ProductStatus status : values()) {
                                if (status.name().equalsIgnoreCase(name)) {
                                        return status;
                                }
                        }
                        throw new IllegalArgumentException("无效状态名称: " + name);
                }
                // 静态方法用于转换状态值
                public static String toChinese(String status) {
                        if ("UNLOCKED".equalsIgnoreCase(status)) {return "未锁定";}
                        if ("LOCKED".equalsIgnoreCase(status)) {return "锁定";}
                        return "未知状态";
                }
        }
}
